/**
 * UninaDocentiSync - A Java application to synchronize Unina resources
 * Copyright (C) 2011  Fabio Scippacercola (nonplay.programmer@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package uninadocenti.framework.synch;

/**
 *
 * @author user
 */
public class SynchingFile<R>
{

    public SynchingFile(R resourceFile, State downloadState, SynchingFile father) {
        this.resourceFile = resourceFile;
        this.downloadState = downloadState;
        this.father = father;
    }

    public void addChildren(SynchingFile<R> c[]) {
        if (children == null)
        {
            children = new SynchingFile[c.length];
            System.arraycopy(c, 0, children, 0, c.length);
        }
        else
        {
            SynchingFile[] oldChildren = children;
            children = new SynchingFile[oldChildren.length + c.length];
            System.arraycopy(oldChildren, 0, children, 0, oldChildren.length);
            System.arraycopy(children, oldChildren.length, children, 0, c.length);
        }

    }

    public SynchingFile getFather() {
        return father;
    }

    protected R resourceFile;
    protected State downloadState;
    protected SynchingFile[] children;
    protected SynchingFile father;

    public static final String PROP_DOWNLOADSTATE = "downloadState";

    public static enum State
    {
        TOSKIP, TODOWNLOAD, DOWNLOADING, DOWNLOADED, SKIPPED
    };

    public boolean isLeaf() {
        return (children == null || children.length == 0);
    }

    public boolean isRoot() {
        return father == null;
    }

    public boolean isProcessed() {
        return !(downloadState == State.TODOWNLOAD || downloadState == State.TOSKIP);
    }

    public boolean hasChildren() {
        return children != null;
    }

    public SynchingFile[] getChildren() {
        return children;
    }

    public State getDownloadState() {
        return downloadState;
    }

    public R getResource() {
        return resourceFile;
    }

    public void setDownloadState(State downloadState) {
        this.downloadState = downloadState;
    }
}
